package com.tvunetworks.center.user.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.model.UserBehavior;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.param.UserOperationParam;
import com.tvunetworks.center.user.service.UserBehaviorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: tvucc-aggregator
 * @description: UserBehaviorController
 * @author lebronchen
 * @create: 2019-03-28 09:45
 **/
@RestController
@Slf4j
@RequestMapping("/userBehavior")
public class UserBehaviorController {


    @Autowired
    private UserBehaviorService userBehaviorService;

    /**
     *
     * @param loginUser loginUser
     * @param userBehavior  userBehavior
     * @return Result
     */
    @PostMapping("/set")
    public Result<String> setUserBehavior(@Logined LoginUser loginUser, UserBehavior userBehavior){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        userBehavior.setUserId(loginUser.getId());
        userBehaviorService.setUserBehavior(userBehavior);
        return ResultUtil.success();
    }

    /**
     *
     * @param loginUser loginUser
     * @param userBehavior userBehavior
     * @return Result
     */
    @GetMapping("/get")
    public Result<Map<String,String>> getUserBehaviors(@Logined LoginUser loginUser, UserBehavior userBehavior){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        userBehavior.setUserId(loginUser.getId());
        return userBehaviorService.getUserBehaviors(userBehavior);
    }

    /**
     *
     * @param loginUser loginUser
     * @param userOperationParam userOperationParam
     * @return Result
     */
    @PostMapping("/listUserOperation")
    public Result<String> listUserOperation(@Logined LoginUser loginUser, @RequestBody UserOperationParam userOperationParam){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        try {
            return userBehaviorService.listUserOperation(userOperationParam,loginUser);
        } catch (Exception e) {
           log.error("listUserOperation",e);
        }
        return ResultUtil.exception();
    }

    /**
     *
     * @param loginUser loginUser
     * @param userOperationParam userOperationParam
     * @return Result
     */
    @PostMapping("/listUserEmail")
    public Result<String> listUserEmail(@Logined LoginUser loginUser, @RequestBody UserOperationParam userOperationParam){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        try {
            return userBehaviorService.listUserEmail(userOperationParam,loginUser);
        } catch (Exception e) {
            log.error("listUserOperation",e);
        }
        return ResultUtil.exception();
    }
}
